package com.dreamcode.caloriestracker.database;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DbHelper {

	public static final String DEVICE_ALERT_ENABLED_ZIP = "DAEZ99";
	public static final String DB_NAME = "caloriestracker.db";	
	public static final String DB_TABLE_ACCOUNTS = "AccountSettings";
	public static final String DB_TABLE_PROFILE = "ProfileDetails";
	public static final int DB_VERSION = 3;
	private static final String CLASSNAME = DbHelper.class.getSimpleName();
	private static String TAG = "DBHelper";
	private static DbHelper dbHelper = null;
	private SQLiteDatabase db = null;

	public SQLiteDatabase getDb() {
		return db;
	}

	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}

	private final DBOpenHelper dbOpenHelper;

	public DbHelper(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context, "WR_DATA", null, 1);
		this.establishDB();
	}
	
	public static DbHelper getInstance(Context context) {
		if (dbHelper == null) {
			dbHelper = new DbHelper(context);
		}
		return dbHelper;
	}
	

	private void establishDB() {
		if (this.db == null) {
			this.db = this.dbOpenHelper.getWritableDatabase();
		}
	}

	public void close(SQLiteDatabase db) {
		try {
			db.close();
		} catch (SQLException e) {
			Log.d(TAG, "close exception: " + e.getLocalizedMessage());
		}
	}
	
	private static class DBOpenHelper extends SQLiteOpenHelper {
		
		private static final String CREATE_TABLE_ACCOUNTS = " create table "
			+ DbHelper.DB_TABLE_ACCOUNTS
			+ " (_id integer primary key autoincrement,"
			+ " type varchar not null, accesstoken varchar, tokensecret varchar);";

		private static final String CREATE_TABLE_PROFILE = " create table "
			+ DbHelper.DB_TABLE_PROFILE
			+ " (_id integer primary key autoincrement,"
			+ " label varchar not null, age varchar, sex varchar, weight varchar, height varchar, waist varchar, paramwe varchar, paramhe varchar, paramwa varchar, data date);";

		public DBOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, DbHelper.DB_NAME, null, DbHelper.DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try {				
				db.execSQL(DBOpenHelper.CREATE_TABLE_ACCOUNTS);
				db.execSQL(DBOpenHelper.CREATE_TABLE_PROFILE);
			} catch (SQLException e) {
				Log.e("", DbHelper.CLASSNAME, e);
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {			
			db.execSQL("DROP TABLE IF EXISTS" + DbHelper.DB_TABLE_ACCOUNTS);
			this.onCreate(db);
			db.execSQL("DROP TABLE IF EXISTS" + DbHelper.DB_TABLE_PROFILE);
			this.onCreate(db);
		}

		@Override
		public void onOpen(SQLiteDatabase db) {
			super.onOpen(db);
		}

	}
}
